<template>
    <div>
        <CommodityComptoents :list="list" :title="title" :pageIndex="pageIndex" :pageSize="pageSize" :pageAll="pageAll"
            @changePageIndex="changePageIndex" @switchChange="switchChange" @changePageSize="changePageSize" @del="del"
            @add="add" @updata="updata" @loadIng="loadIng">
        </CommodityComptoents>
    </div>
</template>
<script setup>
import CommodityComptoents from '../../components/CommodityInformation/CommodityComptoents.vue'
import axios from 'axios'
import { onMounted, ref } from 'vue';
import { ElMessage } from 'element-plus'
let list = ref([])
let title = ref('商品信息')

// 分页个数
let pageSize = ref(5)
// 当前页数
let pageIndex = ref(1)
// 总页数
let pageAll = ref(0)
// 分页
let load = async (val) => {
    let { data } = await axios.get('https://www.vol.com/CommodityComptoents/pageAll', {
        params: {
            pageIndex: pageIndex.value,
            pageSize: pageSize.value,
            name: val
        }
    })
    list.value = data.data
    pageAll.value = data.count
}
// 模糊查询
let loadIng = async (val) => {
    await load(val)
}
// 改变开关状态
let switchChange = async (val) => {
    await axios.get('https://www.vol.com/CommodityComptoents/changeState', {
        params: val
    })
    load()
}
// 请求添加
let add = async (val) => {
    let { data } = await axios.post('https://www.vol.com/CommodityComptoents/add', val)
    if (data) return ElMessage({ message: '添加成功', type: 'success', })
    load()

}
// 删除请求
let del = (val) => {
    if (val.length == 0) return ElMessage({ message: '请选择至少一条进行删除.', type: 'warning', })
    val.forEach(async r => {
        let { data } = await axios.post('https://www.vol.com/CommodityComptoents/del', {
            id: r.id
        })
        if (data) return ElMessage({ message: '删除成功.', type: 'success', })
    })
    load()
}
// 定义接收分页的数据
let changePageIndex = (val) => {
    pageIndex.value = val
    load()
}
// 定义接收分页个数的数据
let changePageSize = (val) => {
    pageSize.value = val
    load()
}
// 定义修改接口
let updata = (val) => {
    axios.post('https://www.vol.com/CommodityComptoents/update', { ...val }).then(({ data }) => {
        if (data) return ElMessage({ message: '修改成功.', type: 'success', })
    })
    load()
}
// 挂载完成
onMounted(() => {
    load()
})
</script>
<style scoped lang='scss'>

</style>